#!/usr/bin/env python3.6
# coding=utf-8

from PyQt5.QtCore import *;
from PyQt5.QtWidgets import *;
from PyQt5.QtGui import *;
# from PyQt5.QtSerialPort import *;
# from serial_thread import SerialQThreadFunction;
import threading;
import sys,Ui_pyui;
import rospy;
from std_msgs.msg import String;
import math;

class myProject(QWidget,Ui_pyui.Ui_Form):
    def __init__(self):
        super().__init__();
        self.setupUi(self);

        #初始报文
        self.da = "100+00+0011111F";

        #ros节点设置
        rospy.init_node("ui", anonymous=True);
        self.msg = String("");
        self.uipub = rospy.Publisher("chatter", String, queue_size=1);

        #创建主线程和串口线程
        # print("主线程", threading.current_thread().ident);       
        # self.Serial_QThread = QThread();
        # self.Serial_QThread_Function = SerialQThreadFunction();
        # self.Serial_QThread_Function.moveToThread(self.Serial_QThread);
        # self.Serial_QThread.start();
        # self.Serial_QThread_Function.SerialFunction.connect(self.Serial_QThread_Function.InitFunction);
        # self.Serial_QThread_Function.SerialFunction.emit();
        # self.Serial_QThread_Function.signal_send1.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send2.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send3.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send4.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send5.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send6.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send7.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send8.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send9.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send10.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_send11.connect(self.Serial_QThread_Function.slot_send);
        # self.Serial_QThread_Function.signal_receive.connect(self.Serial_QThread_Function.slot_receive);
        # self.Serial_QThread_Function.signal_read.connect(self.slot_read);

        self.setWindowTitle("足球辅助训练停球机");

        #调试模式
        self.radioButton.toggled.connect(self.mode_1);
        self.pushButton_101.clicked.connect(self.send_1);
        self.pushButton_102.clicked.connect(self.send_2);
        self.pushButton_103.clicked.connect(self.send_3);
        self.pushButton_104.clicked.connect(self.send_4);
        self.pushButton_105.clicked.connect(self.send_5);
        self.pushButton_106.clicked.connect(self.send_6);
        self.pushButton_107.clicked.connect(self.send_7);
        self.pushButton_108.clicked.connect(self.send_8);
        self.pushButton_109.clicked.connect(self.send_9);
        self.pushButton_110.clicked.connect(self.send_10);
        self.pushButton_111.clicked.connect(self.send_11);
        #自动模式
        self.radioButton_2.toggled.connect(self.mode_2);
        #传感器状态
        self.pushButton_31.toggled.connect(self.receive);
    
    #调试模式
    def mode_1(self):
        if self.radioButton.isChecked() == True:
            self.pushButton_101.setEnabled(True);
            self.pushButton_102.setEnabled(True);
            self.pushButton_103.setEnabled(True);
            self.pushButton_104.setEnabled(True);
            self.pushButton_105.setEnabled(True);
            self.pushButton_106.setEnabled(True);
            self.pushButton_107.setEnabled(True);
            self.pushButton_108.setEnabled(True);
            self.pushButton_109.setEnabled(True);
            self.pushButton_110.setEnabled(True);
            self.pushButton_111.setEnabled(True);
        else:
            self.pushButton_101.setEnabled(False);
            self.pushButton_102.setEnabled(False);
            self.pushButton_103.setEnabled(False);
            self.pushButton_104.setEnabled(False);
            self.pushButton_105.setEnabled(False);
            self.pushButton_106.setEnabled(False);
            self.pushButton_107.setEnabled(False);
            self.pushButton_108.setEnabled(False);
            self.pushButton_109.setEnabled(False);
            self.pushButton_110.setEnabled(False);
            self.pushButton_111.setEnabled(False);

    #发送拨球舵机
    def send_1(self):
        database = self.da;
        value = self.spinBox.value();
        # if value <= -10:
        #     database = database[0] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[4:];
        # elif value > -10 and value < 0:
        #     database = database[0] + "-0" + str(abs(value)) + database[4:];
        if value >= 0 and value < 10:
            database = database[0] + "0" + str(abs(value)) + database[3:];
        else:
            database = database[0] + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[3:];
        self.da = database;

        #发布报文消息
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
        
        # self.Serial_QThread_Function.signal_send1.emit(database);

    #发送相机舵机
    def send_2(self):
        database = self.da;
        value = self.spinBox_2.value();
        if value <= -10:
            database = database[:3] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[6:];
        elif value > -10 and value < 0:
            database = database[:3] + "-0" + str(abs(value)) + database[6:];
        elif value >= 0 and value < 10:
            database = database[:3] + "+0" + str(abs(value)) + database[6:];
        else:
            database = database[:3] + "+" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[6:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send2.emit(database);
    
    #发送步进电机
    def send_3(self):
        database = self.da;
        value = self.spinBox_3.value();
        if value <= -10:
            database = database[:6] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[9:];
        elif value > -10 and value < 0:
            database = database[:6] + "-0" + str(abs(value)) + database[9:];
        elif value >= 0 and value < 10:
            database = database[:6] + "+0" + str(abs(value)) + database[9:];
        else:
            database = database[:6] + "+" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[9:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send3.emit(database);

    #发送摩擦轮1
    def send_4(self):
        database = self.da;
        value = self.comboBox.currentText();
        if value == "档位1":
            database = database[:9] + "1" + database[10:];
        elif value == "档位2":
            database = database[:9] + "2" + database[10:];
        elif value == "档位3":
            database = database[:9] + "3" + database[10:];
        elif value == "档位4":
            database = database[:9] + "4" + database[10:];
        else:
            database = database[:9] + "5" + database[10:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send4.emit(database);

    #发送摩擦轮2
    def send_5(self):
        database = self.da;
        value = self.comboBox_2.currentText();
        if value == "档位1":
            database = database[:10] + "1" + database[11:];
        elif value == "档位2":
            database = database[:10] + "2" + database[11:];
        elif value == "档位3":
            database = database[:10] + "3" + database[11:];
        elif value == "档位4":
            database = database[:10] + "4" + database[11:];
        else:
            database = database[:10] + "5" + database[11:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send5.emit(database);

    #发送电缸
    def send_6(self):
        database = self.da;
        value = self.comboBox_3.currentText();
        if value == "低":
            database = database[:11] + "1" + database[12:];
        elif value == "中":
            database = database[:11] + "2" + database[12:];
        else:
            database = database[:11] + "3" + database[12:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send6.emit(database);
    
    #发送收球电机1
    def send_7(self):
        database = self.da;
        value = self.comboBox_4.currentText();
        if value == "档位1":
            database = database[:12] + "1" + database[13:];
        elif value == "档位2":
            database = database[:12] + "2" + database[13:];
        else:
            database = database[:12] + "3" + database[13:];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send7.emit(database);

    #发送收球电机2
    def send_8(self):
        database = self.da;
        value = self.comboBox_5.currentText();
        if value == "档位1":
            database = database[:13] + "1" + database[14];
        elif value == "档位2":
            database = database[:13] + "2" + database[14];
        else:
            database = database[:13] + "3" + database[14];
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send8.emit(database);
    
    #发送一键发球
    def send_9(self):
        database = self.da;
        value = self.spinBox.value();
        # if value <= -10:
        #     database = database[0] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[4:];
        # elif value > -10 and value < 0:
        #     database = database[0] + "-0" + str(abs(value)) + database[4:];
        if value >= 0 and value < 10:
            database = database[0] + "0" + str(abs(value)) + database[3:];
        else:
            database = database[0] + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[3:];

        value = self.spinBox_2.value();
        if value <= -10:
            database = database[:3] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[6:];
        elif value > -10 and value < 0:
            database = database[:3] + "-0" + str(abs(value)) + database[6:];
        elif value >= 0 and value < 10:
            database = database[:3] + "+0" + str(abs(value)) + database[6:];
        else:
            database = database[:3] + "+" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[6:];

        value = self.spinBox_3.value();
        if value <= -10:
            database = database[:6] + "-" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[9:];
        elif value > -10 and value < 0:
            database = database[:6] + "-0" + str(abs(value)) + database[9:];
        elif value >= 0 and value < 10:
            database = database[:6] + "+0" + str(abs(value)) + database[9:];
        else:
            database = database[:6] + "+" + str(math.floor(abs(value) / 10)) + str(abs(value) % 10) + database[9:];

        value = self.comboBox.currentText();
        if value == "档位1":
            database = database[:9] + "1" + database[10:];
        elif value == "档位2":
            database = database[:9] + "2" + database[10:];
        elif value == "档位3":
            database = database[:9] + "3" + database[10:];
        elif value == "档位4":
            database = database[:9] + "4" + database[10:];
        else:
            database = database[:9] + "5" + database[10:];
        
        value = self.comboBox_2.currentText();
        if value == "档位1":
            database = database[:10] + "1" + database[11:];
        elif value == "档位2":
            database = database[:10] + "2" + database[11:];
        elif value == "档位3":
            database = database[:10] + "3" + database[11:];
        elif value == "档位4":
            database = database[:10] + "4" + database[11:];
        else:
            database = database[:10] + "5" + database[11:];
        
        value = self.comboBox_3.currentText();
        if value == "低":
            database = database[:11] + "1" + database[12:];
        elif value == "中":
            database = database[:11] + "2" + database[12:];
        else:
            database = database[:11] + "3" + database[12:];
        
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send9.emit(database);

    #发送一键收球
    def send_10(self):
        database = self.da;
        value = self.comboBox_4.currentText();
        if value == "档位1":
            database = database[:12] + "1" + database[13:];
        elif value == "档位2":
            database = database[:12] + "2" + database[13:];
        else:
            database = database[:12] + "3" + database[13:];

        value = self.comboBox_5.currentText();
        if value == "档位1":
            database = database[:13] + "1" + database[14];
        elif value == "档位2":
            database = database[:13] + "2" + database[14];
        else:
            database = database[:13] + "3" + database[14];
        
        self.da = database;
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send10.emit(database);

    #发送一键复位
    def send_11(self):
        database = "3";
    
        self.msg = String(database);
        self.uipub.publish(self.msg);
        rospy.loginfo("publish successfully");
    
        # self.Serial_QThread_Function.signal_send11.emit(database);

    #自动模式
    def mode_2(self):
        if self.radioButton_2.isChecked() == True:
            self.pushButton_101.setEnabled(False);
            self.pushButton_102.setEnabled(False);
            self.pushButton_103.setEnabled(False);
            self.pushButton_104.setEnabled(False);
            self.pushButton_105.setEnabled(False);
            self.pushButton_106.setEnabled(False);
            self.pushButton_107.setEnabled(False);
            self.pushButton_108.setEnabled(False);
            self.pushButton_109.setEnabled(False);
            self.pushButton_110.setEnabled(False);
            self.pushButton_111.setEnabled(False);
        
            database = "2";
    
            self.msg = String(database);
            self.uipub.publish(self.msg);
            rospy.loginfo("publish successfully");
        else:
            self.pushButton_101.setEnabled(True);
            self.pushButton_102.setEnabled(True);
            self.pushButton_103.setEnabled(True);
            self.pushButton_104.setEnabled(True);
            self.pushButton_105.setEnabled(True);
            self.pushButton_106.setEnabled(True);
            self.pushButton_107.setEnabled(True);
            self.pushButton_108.setEnabled(True);
            self.pushButton_109.setEnabled(True);
            self.pushButton_110.setEnabled(True);
            self.pushButton_111.setEnabled(True);
    
        # self.Serial_QThread_Function.signal_send11.emit(database);

    #线程执行函数
    def thread_spin(self):
        rospy.spin();

    #下位机回调函数
    def callback(self, msg):
        print(msg.data);

    #接收传感器状态信息
    def receive(self):
        # if self.pushButton_31.isChecked():
        #     self.Serial_QThread_Function.signal_receive.emit(0);
        # else:
        #     self.Serial_QThread_Function.signal_receive.emit(1);
        if self.pushButton_31.isChecked():
            rospy.Subscriber("sen", String, self.callback);

        #开启线程
        t = threading.Thread(target=self.thread_spin);
        t.start();
        
    # def slot_read(self,data):
    #     Byte_data = bytes(data);
    #     re_da = Byte_data.decode('utf-8', 'ignore');
    #     print(re_da[0]);

    #     if re_da[0] == "0":
    #         self.label_301.setStyleSheet('''QLabel{border-radius: 10px; background-color: green;}''');
    #     else:
    #         self.label_301.setStyleSheet('''QLabel{border-radius: 10px; background-color: red;}''');

if __name__ == '__main__':
    app = QApplication(sys.argv);
    main = myProject();
    main.show();
    app.exec_();
    sys.exit();